package com.demo.service.impl;


import com.demo.mapper.UserMapper;
import com.demo.pojo.Result;
import com.demo.pojo.User;
import com.demo.service.UserService;
import com.demo.service.UserService2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl2 implements UserService2 {
    @Autowired
    private UserMapper userMapper;
    @Override
    public Result<User> getUser(Integer id) {
        User user = this.userMapper.getUserById(id);
        if(user == null){
            return Result.fail("没有查询到数据");
        }else{
            return Result.success(user);
        }
    }

    @Override
    public Result<List<User>> getAll() {
        List<User> users = this.userMapper.getAll();
        Result<List<User>> result = new Result<>();
        if(users != null && users.size() > 0){
            result.setCode(2000);
            result.setMessage("查询成功");
            result.setData(users);
        }else{
            result.setCode(4004);
            result.setMessage("没有查询到数据");
        }
        return result;
    }

    @Override
    public Result<Void> deleteUser(Integer id) {
        Integer x = this.userMapper.deleteUser(id);
        Result<Void> result = new Result<>();
        if(x>0){
            result.setCode(2000);
            result.setMessage("删除成功");
        }else{
            result.setCode(4000);
            result.setMessage("删除失败");
        }
        return result;
    }
}
